package com.wht.media.controller;


import com.wht.media.common.R;
import com.wht.media.model.param.*;
import com.wht.media.model.result.MediaInfoResult;
import com.wht.media.model.result.RtpServerResult;
import com.wht.media.model.result.Statistic;
import com.wht.media.model.result.StreamUrlResult;
import com.wht.media.service.IApiService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 流媒体Api
 *
 * @author by HaiTao.Wang on 2025/9/12.
 */
@Controller
@Mapping("api")
public class ApiController {

    @Inject
    IApiService iApiService;

    /**
     * 添加rtmp/rtsp拉流代理
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("addStreamProxy")
    public R addStreamProxy(@Validated @Body StreamProxyParam param) {
        StreamUrlResult result = iApiService.addStreamProxy(param);
        return R.data(result);
    }

    /**
     * 关闭拉流代理
     *
     * @param key
     * @return
     */
    @Post
    @Mapping("delStreamProxy")
    public R delStreamProxy(String key) {
        Boolean flag = iApiService.delStreamProxy(key);
        return R.data(flag);
    }

    /**
     * 添加rtmp/rtsp推流代理
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("addStreamPusherProxy")
    public R addStreamPusherProxy(@Validated @Body StreamPushProxyParam param) {
        String error = iApiService.addStreamPusherProxy(param);
        return R.data(error);
    }

    /**
     * 删除rtmp/rtsp推流代理
     *
     * @param key
     * @return
     */
    @Post
    @Mapping("delStreamPusherProxy")
    public R delStreamPusherProxy(String key) {
        Boolean flag = iApiService.delStreamPusherProxy(key);
        return R.data(flag);
    }


    /**
     * 关闭流
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("closeStream")
    public Result<Integer> closeStream(@Validated @Body CloseStreamParam param) {
        Integer status = iApiService.closeStream(param);
        return new Result<>(status);
    }

    /**
     * 关闭流(批量关)
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("closeStreams")
    public R closeStreams(@Validated @Body CloseStreamsParam param) {
        Integer status = iApiService.closeStreams(param);
        return R.data(status);
    }

    /**
     * 获取流列表
     *
     * @param param
     * @return
     */
    @Mapping("getMediaList")
    public R getMediaList(GetMediaListParam param) {
        List<MediaInfoResult> list = iApiService.getMediaList(param);
        return R.data(list);
    }

    /**
     * 获取流信息
     *
     * @param param
     * @return
     */
    @Mapping("getMediaInfo")
    public R getMediaInfo(@Validated MediaQueryParam param) {

        // 是否在线
        Boolean mediaOnline = iApiService.isMediaOnline(param);

        if (!mediaOnline) {
            return R.error("当前流不在线");
        }

        MediaInfoResult info = iApiService.getMediaInfo(param);
        return R.data(info);
    }


    /**
     * 流是否在线
     *
     * @param param
     * @return
     */
    @Mapping("isMediaOnline")
    public R isMediaOnline(@Validated MediaQueryParam param) {
        Boolean online = iApiService.isMediaOnline(param);
        return R.data(online);
    }


    /**
     * 开始录像
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("startRecord")
    public R startRecord(@Validated @Body StartRecordParam param) {
        Boolean flag = iApiService.startRecord(param);
        return R.data(flag);
    }


    /**
     * 停止录像
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("stopRecord")
    public R stopRecord(@Validated @Body StopRecordParam param) {
        Boolean flag = iApiService.stopRecord(param);
        return R.data(flag);
    }

    /**
     * 是否录像
     *
     * @param param
     * @return
     */
    @Mapping("isRecording")
    public R isRecording(@Validated RecordStatusParam param) {
        Boolean flag = iApiService.isRecording(param);
        return R.data(flag);
    }

    /**
     * 获取内存资源信息
     *
     * @return
     */
    @Mapping("getStatistic")
    public R getStatistic() {
        Statistic statistic = iApiService.getStatistic();
        return R.data(statistic);
    }


    /**
     * 获取服务器配置
     *
     * @return
     */
    @Mapping("getServerConfig")
    public R getServerConfig() {
        String confStr = iApiService.getServerConfig();
        return R.data(confStr);
    }

    /**
     * 重启流媒体服务
     *
     * @return
     */
    @Mapping("restartServer")
    public R restartServer() {
        Boolean status = iApiService.restartServer();
        return R.data(status);
    }


    /**
     * 开启rtp服务
     *
     * @param param
     * @return
     */
    @Post
    @Mapping("openRtpServer")
    public R openRtpServer(@Validated @Body OpenRtpServerParam param) {
        Integer port = iApiService.openRtpServer(param);
        return R.data(port);
    }

    /**
     * 关闭rtp服务
     *
     * @param stream
     * @return
     */
    @Post
    @Mapping("closeRtpServer")
    public R closeRtpServer(@NotBlank(message = "流id不为空") @Param(value = "stream") String stream) {
        Integer status = iApiService.closeRtpServer(stream);
        return R.data(status);
    }

    /**
     * 获取所有RTP服务器
     *
     * @return
     */
    @Mapping("listRtpServer")
    public R listRtpServer() {
        List<RtpServerResult> results = iApiService.listRtpServer();
        return R.data(results);
    }

}
